<?php
/**
 * Geocontexter
 * @link http://code.google.com/p/geocontexter/
 * @package Geocontexter
 */

/**
 * Admin index controller
 *
 *
 * @package Geocontexter
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author Armand Turpel <geocontexter@gmail.com>
 * @version $Rev$ / $LastChangedDate$ / $LastChangedBy$
 */

namespace Gospecies\Controller;

use Zend\View\Model\JsonModel;
use Gospecies\Controller\AbstractIdentityJsonController;


class AjaxAddProjectJsonController extends AbstractIdentityJsonController
{

    public function indexAction()
    {
        // get logged user
        $this->user = $this->get_identity();

        try {
              $error  = array();
              $params = array();

              // check permission if id_group is "contributor" or above.
              // users of those groups can only update own projects
              //
              if ((isset($this->user->id_group) && ((int)$this->user->id_status == 200))) {
                  if ((int)$this->user->id_group >= 400) {
                    return new JsonModel(array('error' => 'permission denied'));
                  }
              } else {
                  return new JsonModel(array('error' => 'permission denied'));
              }

              $params['data'] = array('title'       => $this->request->getPost()->project_name,
                                      'id_status'   => 400,
                                      'id_owner'    => $this->user->id_user,
                                      'id_modifier' => $this->user->id_user,
                                      'lang'        => 'en'
                                      );

              if (empty($params['title']) || ($params['title'] === null)) {
                  $error[] = 'Project name isnt defined or empty';
              }

              $ProjectAdd = $this->CoreModel('ProjectAdd');

              $id_project  = $ProjectAdd->run( $params );

              $attr_groups = $this->request->getPost()->id_attr_groups;

              if (is_array($attr_groups) && (count($attr_groups) > 0)) {

                  $ProjectAddAttributeGroups = $this->CoreModel('ProjectAddAttributeGroups');

                  $params  = array('id_project'     => $id_project[0]['id_project'],
                                   'id_attr_groups' => $attr_groups);

                  $result  = $ProjectAddAttributeGroups->run( $params );
              }

              $ProjectAddList = $this->CoreModel('ProjectAddList');

              $lists = $this->request->getPost()->id_lists;

              if (is_array($lists)) {

                  $_lists = array();

                  foreach ($lists as $key => $list) {
                    $_lists[] = (string)$list;
                  }

                  $params  = array('id_project' => (string)$id_project[0]['id_project'],
                                   'id_list'    => $_lists);

                  $result = $ProjectAddList->run( $params );
              }

              $ProjectAddUsers = $this->CoreModel('ProjectAddUsers');

              $params  = array('id_project' => (string)$id_project[0]['id_project'],
                               'id_user'    => array($this->user->id_user));

              $result  = $ProjectAddUsers->run( $params );

              return new JsonModel(array('id_project' => $id_project));

        } catch (\Exception $e) {

            $this->default_error = "Error\n File: " . __file__ . "\nLine: " . __line__ . "\nError: " . $e->__toString();

            return new JsonModel(array('error' => $this->default_error));

        }
    }
}
